﻿using EDSDKWrapper.Framework.Managers;
using EDSDKWrapper.Framework.Objects;
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Linq;

namespace Vtron.LEDCorrective
{
    /// <summary>
    /// LiveViewWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LiveViewWindow : Window
    {
        public LiveViewWindow()
        {
            InitializeComponent();

            Loaded += LiveViewWindow_Loaded;
        }

        //SDKHandler CameraHandler;
        public Task LiveViewCapturingTask { get; set; }
        public Camera Camera { get; set; }
        public FrameworkManager FrameworkManager { get; set; }
        TaskScheduler UITaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
        void LiveViewWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.FrameworkManager = new FrameworkManager();
            this.LiveViewCapturingTask = Task.Factory.StartNew(() =>
            {
                this.Camera = this.FrameworkManager.Cameras.First();
                this.Camera.StartLiveView();

                while (Camera.LiveViewEnabled)
                {
                    int exceptionCount = 0;
                    try
                    {
                        var stream = this.Camera.GetLiveViewImage();

                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();
                        bitmapImage.StreamSource = stream;
                        bitmapImage.EndInit();
                        bitmapImage.Freeze();

                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            this.live_Image.Source = bitmapImage;
                        }));

                        exceptionCount = 0;
                    }
                    catch
                    {
                        Thread.Sleep(100);
                        if (++exceptionCount > 10)
                        {
                            throw;
                        }
                    }
                }

                Console.Write("");

            }).ContinueWith((previewsTask) =>
            {
                if (previewsTask.IsFaulted)
                {
                    throw previewsTask.Exception;
                }
            },
                UITaskScheduler);


            //CameraHandler = SDKHandler.Instance;
            //CameraHandler.LiveViewUpdated += new SDKHandler.ImageUpdate(SDK_LiveViewUpdated);

            //CameraHandler.CloseSession();
            //CameraHandler.OpenSession(CameraHandler.GetCameraList()[0]);

            //if (!CameraHandler.IsEvfFilming)
            //{
            //    if (!CameraHandler.IsLiveViewOn)
            //    {
            //        CameraHandler.StartLiveView();
            //    }
            //    else
            //    {
            //        CameraHandler.StopLiveView();
            //    }
            //}
        }

        //private void SDK_LiveViewUpdated(Image img)
        //{
        //    //i++;
        //    //if (i > 1)
        //    //    return;
        //    if (CameraHandler.IsLiveViewOn)
        //    {
        //        try
        //        {
        //            img.Save("d://img001.jpg");
        //            Dispatcher.BeginInvoke(new Action(() =>
        //               {
        //                   live_Image.Source = new BitmapImage(new Uri("d://img001.jpg"));
        //               }));

        //            //using (var ms = new MemoryStream())
        //            //{
        //            //    System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage();
        //            //    bitmap.BeginInit();

        //            //    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        //            //    ms.Seek(0, System.IO.SeekOrigin.Begin);
        //            //    bitmap.StreamSource = ms;
        //            //    bitmap.EndInit();
        //            //    bitmap.Freeze();

        //            //    Dispatcher.BeginInvoke(new Action(() =>
        //            //       {
        //            //           live_Image.Source = bitmap;
        //            //       }));
        //            //}
        //        }
        //        catch (Exception ex)
        //        {
        //            MessageBox.Show("111" + ex.Message);
        //        }
        //    }
        //}
    }
}
